package simplelab;

import jason.asSemantics.*;
import jason.asSyntax.*;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

/*
 * This class is responsible for the "katz.Action(A,B,C)"
 * function in Jason.
 */
public class Action extends DefaultInternalAction {

    // My Knowledge
    public static World world = new World();
    private boolean myTeamIDSet = false;

    @Override
    public Object execute(TransitionSystem ts, final Unifier un, final Term[] arg) throws Exception {
        //final Beliefs beliefs = new Beliefs(ts.getAg().getBB().toString());

        Agent nowAgent = new Agent();
        Beliefs beliefs = new Beliefs();

        Iterator<Literal> iterable = ts.getAg().getBB().iterator();
        
        Literal next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         System.out.println("ez is " + next.getFunctor().toString());          

//        while(iterable.hasNext()){
//            System.out.println(next.getFunctor().toString());
//            next = iterable.next();
//        }
        ArrayList<Agent> agents = new ArrayList<Agent>();
        ArrayList<Food> foods = new ArrayList<Food>();
        for (int k = 0; k < next.getTerms().size(); k++) {
            String foodText = next.getTerm(k).toString();
            if (foodText.charAt(0) == '[') {
                int i = 1;
                while (foodText.charAt(i) != ']') {
                    if (foodText.charAt(i) == ',') {
                        i++;
                    }
                    i++;
                    int distance;
                    int value;
                    int p_x, p_y;

                    int nextComa = foodText.indexOf(',', i);
                    distance = Integer.parseInt(foodText.substring(i, nextComa));
                    i = nextComa + 1;
                    nextComa = foodText.indexOf(',', i);
                    value = Integer.parseInt(foodText.substring(i, nextComa));
                    i = nextComa + 1;
                    nextComa = foodText.indexOf(',', i);
                    p_x = Integer.parseInt(foodText.substring(i, nextComa));
                    i = nextComa + 1;
                    nextComa = foodText.indexOf(']', i);
                    p_y = Integer.parseInt(foodText.substring(i, nextComa));
                    i = nextComa + 1;
                    Food food = new Food(value, new Position(p_x, p_y));
//                    //System.out.println("action food " + food.toString());
                    beliefs.foods.add(food);
                    foods.add(food);
                }
            }
        }
        ////System.out.println("beliegs " + beliefs.foods.toString());
        try{
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        beliefs.teamTimeLeft = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        nowAgent.energy = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        beliefs.teammates[0] = next.getTerm(0).toString();
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        nowAgent.direction = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        World.turn = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        nowAgent.team = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        nowAgent.id = Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        if (next.getFunctor().toString().equals("mapsize")) {
            int x = Integer.parseInt(next.getTerm(0).toString());
            int y = Integer.parseInt(next.getTerm(1).toString());

            beliefs.mapSize = new Point(x, y);
            //System.out.println("actionban volt map change");
            next = iterable.next();
        }
        beliefs.name = next.getTerm(0).toString();
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        beliefs.ratio = Double.parseDouble(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        String agentText = next.getTerm(0).toString();
        if (agentText.charAt(0) == '[') {
            int i = 1;
            while (agentText.charAt(i) != ']') {
                if (agentText.charAt(i) == ',') {
                    i++;
                }
                i++;
                int distance;
                int a_id;
                int a_team;
                int a_energy;
                int p_x, p_y;
                int orientation;

                int nextComa = agentText.indexOf(',', i);
                distance = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(',', i);
                a_id = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(',', i);
                a_team = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(',', i);
                a_energy = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(',', i);
                p_x = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(',', i);
                p_y = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                nextComa = agentText.indexOf(']', i);
                orientation = Integer.parseInt(agentText.substring(i, nextComa));
                i = nextComa + 1;
                Agent agent = new Agent(a_id, a_team, a_energy, new Position(p_x, p_y), orientation);
                agents.add(agent);
            }

        }
        while ((next = iterable.next()).getFunctor().equalsIgnoreCase("teammate")) {
           // next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());         
            //beliefs.inWater = Boolean.parseBoolean(next.getTerm(0).toString());
        }
        //next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         //System.out.println("ez is " + next.getFunctor().toString());          
        beliefs.attackerID = (next.getTerm(0).toString().equalsIgnoreCase("[]")) ? -1 : Integer.parseInt(next.getTerm(0).toString());
        next = iterable.next(); //System.out.println(" kiír" + next.getTerm(0).toString());         System.out.println("ez is " + next.getFunctor().toString());          
//        if(true){//TODO
        int x = Integer.parseInt(next.getTerm(0).toString());
        int y = Integer.parseInt(next.getTerm(1).toString());
        nowAgent.position = new Position(x, y);
//        }
        }catch (NoSuchElementException e){
            e.printStackTrace();
            e.toString();
            iterable = ts.getAg().getBB().iterator();
            while(iterable.hasNext()){
                next = iterable.next();
                System.out.println(next.getFunctor().toString());
            }
            
        }
        
        
        /*while(iterable.hasNext()){
        next=iterable.next();
        System.out.println("egy " +  next.getFunctor());
        for(int i = 0; i < next.getTerms().size(); i++){
        System.out.println("darab " + i + " " + next.getTerm(i).toString());
        }
        }*/
        
//        System.out.println("asdf " + nowAgent.position.toString());

        agents.add(nowAgent);
//        System.out.println("now ag" + nowAgent);
        if (myTeamIDSet == false) {
            world.myTeamID = nowAgent.team;
            myTeamIDSet = true;
        }
        //if(World.Friendlies.size() > 0)
//        System.out.println("3 " + World.Friendlies.toString()+ " " + nowAgent);
        world.refreshAgents(nowAgent, agents);
        
//        // a poziciók frissítése 
//        for (int i = 0; i < World.Friendlies.size(); i++) {
//            if (nowAgent.id == World.Friendlies.get(i).id) {
//                nowAgent = World.Friendlies.get(i);
//                break;
//            }
//        }
        world.refreshInsideFood(foods);
//        beliefs.nowAgent = nowAgent;
//
//        World.nowAgent = nowAgent;




//        System.out.println("4 " + World.Friendlies.get(0).toString()+ " " + World.nowAgentIDInFriendlies);
        Command command =
                world.Manage(beliefs);
//        System.out.println("size " +  World.worldHeight +  " " + World.worldWidth);
//        if (World.worldReSize) {
//            World.worldReSize = false;
//            World.fillEmptyBlocks();
//        }

            if (world.guiOn) {
                DebugMap.mcanvas.paint(DebugMap.mcanvas.getGraphics(), World.foods);
            }
        
//        System.out.println("5 " + World.Friendlies.get(0).toString() + " " + World.nowAgentIDInFriendlies);
        //RefreshDebugWin();
		/*javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        debugMap.createAndShowGUI(beliefs.pos, beliefs.dir);
        }
        });*/

        NumberTermImpl actionType = new NumberTermImpl(command.actionType.ordinal()),
                p1 = new NumberTermImpl(command.param1),
                p2 = new NumberTermImpl(command.param2);

        return (un.unifies(actionType, arg[0]) && un.unifies(p1, arg[1])
                && un.unifies(p2, arg[2]));
    }
}